<?php
namespace api\models\base;

use Yii;
use yii\base\Model;

/**
 * Class BaseForm
 * @package api\models\base
 *
 * 基础表单模型,公用方法封装
 */
class BaseForm extends Model
{

    /**
     * 遍历创建文件目录
     *
     * @param string $dir_path
     * @return array( 'status', 'msg' )
     */
    public function sp_mkdirs( $dir_path = '' )
    {
        try{

            $ret2return  = [ 'status' => false,'msg' => '目录创建失败' ];
            // 目录已存在 || mkdir没加true是上一级目录不存在就会创建失败
            if( is_dir( $dir_path ) || mkdir( $dir_path, 0755, true ) ){
                $ret2return  = [ 'status' => true,'msg' => '目录创建成功' ];
            }

        }catch ( \Exception $e ){
            // TODO: Apach PHP4log单独记录错误日志
            $ret2return  = [
                'status' => false,
                'msg' => '目录创建失败,原因如下: ' . $e->getTraceAsString(),
                'error_code' => $e->getCode(),
            ];
        }

        return $ret2return;
    }

    /**
     * @name getClientIp
     * @desc 获得客户端ip
     * @return  string client ip
     */
    public function getClientIp()
    {
        if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $onlineip = getenv('HTTP_CLIENT_IP');
        } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $onlineip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $onlineip = getenv('REMOTE_ADDR');
        } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $onlineip = $_SERVER['REMOTE_ADDR'];
        }
        return $onlineip;
    }


    /*********************************************************************
    函数名称:encrypt
    函数作用:加密解密字符串
    使用方法:
    加密    :encrypt('str','E','nowamagic');
    解密    :encrypt('被加密过的字符串','D','nowamagic');
    参数说明:
    $string  :需要加密解密的字符串
    $operation:判断是加密还是解密:E:加密  D:解密
    $key      :加密的钥匙(密匙);
     *********************************************************************/
    function rc_encrypt($string,$operation)
    {
        $key = $this->signKey();
        $key_length = strlen($key);

        if( $operation == 'D' ){
            $string = base64_decode( $string );
        }else{
            $string = json_encode( $string );
            $string = substr( md5($string.$key),0,8 ).$string;
        }
        $string_length = strlen( $string );

        $rndkey = $box = array();
        $result = '';

        for($i=0;$i<=255;$i++)
        {
            $rndkey[$i]=ord($key[$i%$key_length]);
            $box[$i]=$i;
        }

        for($j=$i=0;$i<256;$i++)
        {
            $j = ( $j+$box[ $i ]+$rndkey[$i])%256;
            $tmp = $box[ $i ];
            $box[ $i ] = $box[ $j ];
            $box[ $j ] = $tmp;
        }

        for($a=$j=$i=0;$i<$string_length;$i++)
        {
            $a = ( $a+1 ) % 256;
            $j = ( $j+$box[ $a ] ) % 256;
            $tmp = $box[ $a ];
            $box[ $a ] = $box[$j];
            $box[ $j ] = $tmp;
            $result .= chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));
        }

        if($operation=='D')
        {
            if(
                substr($result,0,8) ==
                substr(md5(substr($result,8).$key),0,8)
            ){
                return json_decode( substr($result,8), true );
            }
            else{
                return'';
            }
        }
        else
        {
            return str_replace('=','',base64_encode($result));
        }
    }

    /**
     * 加解密的密钥
     * @return string
     */
    private function signKey()
    {
        return pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");
    }

}
